package com.ght.springboot.work;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @description:
 * @author: haitao.gao
 * @create: 2019-05-16 15:33
 **/
public class StoreReceivable {

    public static void main(String[] args) throws IOException, ParseException, InterruptedException {

        String s = "2018-12-05";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        StoreReceivable storeReceivable = new StoreReceivable();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(simpleDateFormat.parse(s));
        Boolean isCountion = true;
        while (isCountion) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            Long start = System.currentTimeMillis();
            HttpResponse response = storeReceivable.exec(simpleDateFormat.format(calendar.getTime()));
            Long end = System.currentTimeMillis();
            if (response.getStatusLine().getStatusCode() != 200) {
                isCountion = false;
                System.out.print(response);
            } else {
                System.out.print("同步时间【" + simpleDateFormat.format(calendar.getTime()) + "】，花费时间【" + (end - start) / 1000 + "】秒" + "\n");
                Thread.sleep(10 * 1000);
            }
        }
    }

//        System.out.print(response);
//    }
   // curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: ' ''

    public HttpResponse exec(String date) throws IOException {

        CloseableHttpClient httpClient = HttpClients.custom()
                // 设置重试次数
                .setRetryHandler(new DefaultHttpRequestRetryHandler(3, false))
                .build();
        HttpPost httpPost = new HttpPost("http://120.27.231.23:19009/v1/storeReceivableBill/generateBill?startDate="+date);

        httpPost.addHeader(new BasicHeader("Content-Type", "application/json"));
        httpPost.addHeader(new BasicHeader("Authorization","Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTk3ODY0MTMsInVzZXJfbmFtZSI6InN5c3RlbSIsImF1dGhvcml0aWVzIjpbIkVSUDpzeXN0ZW0iLCJaYnlQcmluY2lwYWw6ZXlKaVlYTnBZMVZ6WlhJaU9uc2lkWE5sY2tsa0lqbzVPU3dpZFhObGNrTnZaR1VpT2lKemVYTjBaVzBpTENKMWMyVnlUbUZ0WlNJNkluTjVjM1JsYlNJc0luQmxjbk52Yms1aGJXVWlPaUxucnFIbmtJYmxrWmdpTENKd1pYSnpiMjVKWkNJNk5UWXdMQ0p5YjJ4bFRHbHpkQ0k2VzNzaWNtOXNaVWxrSWpveE56QXNJbkp2YkdWRGIyUmxJam9pYzNsemRHVnRJaXdpY205c1pVNWhiV1VpT2lMa3VKcmxpcUhsaGFqbW5ZTWlmVjBzSW1SbGRtbGpaU0k2Ym5Wc2JDd2lhWE5RWkdFaU9tNTFiR3g5ZlE9PSJdLCJqdGkiOiI5YzBhYmNlZC00MWQ3LTRmNzAtYjBmOS05MWY2ZTUzZWVkOWIiLCJjbGllbnRfaWQiOiJ6YnktY2xvdWQiLCJzY29wZSI6WyJvcGVuaWQiXX0.jS2AKMiZ-fiVrtRTK-29qu6oDaelj0MOGNuQPlzw2td7MmHJGfRyps4VC4PgOyR9hXDxBjwHUFUrV368-2wbBP9GVVGppv1gWOwX7rybMBKZ3PRrG-oU0F4EqDgju31dQ-gcghDp1z8wBrNa1IMYLnGEioH7WiZ6CAQ0u5Ke5QjyWunH3mJWXnbqHP0X74wozAdTZFFPJYTJgib-u7rG1J3h0pss54PZZNIzGnDfOF4F3g3kZNtgO-iqXKWaBxjyspB6gcHLd7SEFrU9JgfyTDm95TG6PYJRyn_k2v4obsBJpLbz_Pw8GJxXRmBr5nYKwx6ejkcxbjFKHhDcaEQ86A"));
//        StringEntity s = new StringEntity(null, Charset.forName("UTF-8"));
//        httpPost.setEntity(s);
        return httpClient.execute(httpPost);
    }
}
